package com.dz.lm.web.controller.newuser.agentreview;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.agentreview.AgentReviewService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 申请审核控制层
 */
@Controller
@RequestMapping(value = "/h5/weijie/rewriteUrl/agentReview", produces = "application/json")
public class AgentReviewController extends BasicControl {
    @Autowired
    private AgentReviewService agentReviewService;

    /**
     * 审批列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //  String uId = request.getAttribute("uersId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);

        //处理人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //staus 0待处理
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = agentReviewService.query(pageNum + 1, rows,userId,status);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 审批列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/get")
    public Map get(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //  String uId = request.getAttribute("uersId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);

        //
        Integer reivewId = param.get("reviewId") != null ? Integer.parseInt(param.get("reviewId").toString()) : 0;

        data = agentReviewService.get(reivewId,0,true);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 处理代理审批
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/review")
    public Map review(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //  String uId = request.getAttribute("uersId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);

        Integer reviewId = param.get("reviewId") != null ? Integer.parseInt(param.get("reviewId").toString()) : 0;
        //staus 0待处理 1审核通过
        //     * -1 审核未通过
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        //评论
        String remark = param.get("remark") != null ? param.get("remark").toString() : "";
        //代理级别
        String levelNum = param.get("levelNum") != null ? param.get("levelNum").toString() : "";

        //审核添加返点
        double returnPoint = param.get("returnPoint") != null ? StringUtil.getDouble(param.get("returnPoint").toString(),-1) : -1;
        //代理折扣
        double agentSale = param.get("agentSale") != null ? com.smartlbs.util.StringUtil.getDouble(param.get("agentSale").toString(),-1): -1;
        //申请通过推送
        String agentContent = param.get("agentContent") != null ? param.get("agentContent").toString() : "";
        int i = agentReviewService.doReview(reviewId, status, remark,agentContent,returnPoint,agentSale,levelNum);
        if(i >0){
            message = "操作成功";
            result = "00";
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

}
